package top.appx.zutil;

/**
 * Created by john on 2016/7/16.
 */
public class IpUtil {
    public static long ip2long(String ip)
    {
        String[] ips = ip.split("\\.");
        long num = 16777216L * Long.parseLong(ips[0]) + 65536L * Long.parseLong(ips[1]) + 256 * Long.parseLong(ips[2]) + Long.parseLong(ips[3]);
        return num;
    }


    public static String long2ip(long ipLong)
    {
        //long ipLong = 1037591503;
        long[] mask = new long[] { 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 };
        long num = 0;
        StringBuilder ipInfo = new StringBuilder();
        for (int i = 0; i < 4; i++)
        {
            num = (ipLong & mask[i]) >> (i * 8);
            if (i > 0) ipInfo.insert(0, ".");
            ipInfo.insert(0, Long.parseLong(num+""));
        }
        return ipInfo.toString();
    }
}
